TOOLPREFIX = riscv32-unknown-elf-
CC = $(TOOLPREFIX)gcc
LD = $(TOOLPREFIX)ld

CFLAGS = -Wall -Werror -fno-omit-frame-pointer
CFLAGS += -mcmodel=medany
CFLAGS += -ffreestanding -fno-common -nostdlib -mno-relax
CFLAGS += -I.
CFLAGS += -march=rv32g

LDFLAGS = -z max-page-size=4096

OBJS = \
	entry.o \
	cons.o \
	elfloader.o \
	file.o \
	initcode.o \
	kalloc.o \
	kernelvec.o \
	kobj.o \
	kvm.o \
	lldrv.o \
	lock.o \
	main.o \
	pipe.o \
	portal.o \
	printk.o \
	proc.o \
	putfunc.o \
	start.o \
	syscall.o \
	trampoline.o \
	trap.o \
	traphdlr.o \
	util.o
	 
kernel : $(OBJS)
	$(LD) $(LDFLAGS) -T kernel.ld -o kernel $(OBJS)

entry.o : entry.S
	$(CC) $(CFLAGS) -c entry.S -o entry.o

initcode.o : initcode.S
	$(CC) $(CFLAGS) -c initcode.S -o initcode.o

kernelvec.o : kernelvec.S
	$(CC) $(CFLAGS) -c kernelvec.S -o kernelvec.o

lldrv.o : lldrv.S
	$(CC) $(CFLAGS) -c lldrv.S -o lldrv.o

portal.o : portal.S
	$(CC) $(CFLAGS) -c portal.S -o portal.o

# printk.o : printk.S
# 	$(CC) $(CFLAGS) - c printk.S -o printk.o

trampoline.o : trampoline.S
	$(CC) $(CFLAGS) -c trampoline.S -o trampoline.o

clean:
	rm *.o kernel
